/**
 * CodeDisplayTag.java
 * Product:Performance
 * Version:1.0
 * Copyright 2009 by DNE
 * All Rights Reserved.
 */
package com.site.util.taglib;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.site.util.Code;
import com.site.util.MessageUtils;
import com.site.util.StringUtils;


/**
 * @author zhangzhi
 * 根据code xml文件中的配制的值，显示对应的国际化文本。
 * 例如：
 * <code-type name="Performance_Position">
		<option code="1">程序员</option>
		<option code="2">项目经理</option>
	</code-type>
	
	如果传进来的value=1,name=Performance_Position,该标签将显示程序员
 */
public class CodeDisplayTagXML extends TagSupport {

	private static final long serialVersionUID = 1L;

	private String name;
	
	private String value;
	
	public int doStartTag() throws JspException {
		if(!StringUtils.isEmpty(name)){
			List<Code> codeList=MessageUtils.getCodeList(name,(HttpServletRequest)pageContext.getRequest());
			StringBuffer html=new StringBuffer("");
			for (Iterator iterator = codeList.iterator(); iterator.hasNext();) {
				Code code = (Code) iterator.next();
				if(code.getCode().equalsIgnoreCase(value)){
					html.append(code.getText());
					break;
				}
			}
			try {
				pageContext.getOut().write(html.toString());
			} catch (IOException e) {
				e.printStackTrace();
				System.err.println("CodeTag标签出错！");
			}
		}
		
		return super.doStartTag();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
}
